package com.bwie.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bwie.mapper.TbProdMapper;
import com.bwie.pojo.TbProd;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class ProdDown {
    @Autowired
    TbProdMapper tbProdMapper;
    /**
     * 每分钟检查一次商品下架
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void prodDownTask(){
        log.info("开始执行商品下架任务");
        List<TbProd> tbProds = tbProdMapper.selectList(
                new QueryWrapper<TbProd>()
                        .lambda()
                        .eq(TbProd::getDownType, 1)
                        .eq(TbProd::getProdStatus, 1)
                        .le(TbProd::getDownTime,new Date())
        );

        log.info("待下架商品数量：{}",tbProds.size());

        for (TbProd tbProd : tbProds) {
            tbProd.setProdStatus(0);
            tbProdMapper.updateById(tbProd);
        }

        log.info("商品下架任务执行完毕~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

    }
}
